/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.UserResultController;
import java.awt.GridBagConstraints;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import model.Observable;
import model.Observer;
import model.Person;
import model.User;

/**
 *
 * @author Ira
 */
public class UserResultView extends JScrollPane implements Observer, Observable {

	private List<Observer> obs = new ArrayList<Observer>();
	private JTable tabResults;
	private Person[] users = null;
	private UserResultController controller;
	private View mainWindow;
	private UserCellRenderer userRenderer = new UserCellRenderer();
	private UserLateCellRenderer userLateRenderer = new UserLateCellRenderer();
	
	public UserResultView(View mainWindow) {
		this.controller = new UserResultController(this);
		
		this.mainWindow = mainWindow;
		this.tabResults = new JTable(new TableUser(this.users));
		
		this.tabResults.addMouseListener(this.controller);
		
		this.setViewportView(this.tabResults);
	}
	
	public void majResult(Person[] users) {
		this.users = users;
		this.tabResults = new JTable(new TableUser(users));
		this.setViewportView(this.tabResults);
		this.tabResults.getColumnModel().getColumn(4).setCellRenderer(this.userRenderer);
		this.tabResults.getColumnModel().getColumn(5).setCellRenderer(this.userRenderer);
		this.tabResults.getColumnModel().getColumn(6).setCellRenderer(this.userLateRenderer);
		this.tabResults.addMouseListener(this.controller);
		
		this.mainWindow.setUserSearchView();
	}

	@Override
	public void update(Object items) {
		try {
			this.majResult(((Set<Person>)items).toArray(new Person[0]));
		}
		catch(Exception e) {
			
		}
	}

	@Override
	public void addObserver(Observer obs) {
		this.obs.add(obs);
	}

	@Override
	public void updateObservers(Object data) {
		if(this.tabResults.getSelectedRow() != -1) {
			for(Observer obs: this.obs) {
				obs.update(this.users[this.tabResults.getSelectedRow()]);
			}
		}
	}

	@Override
	public void delObserver(Observer obs) {
		this.obs.remove(obs);
	}
}
